﻿using System;
using SharpSqlAnalyzer.nodes;
using SharpSqlAnalyzer.pp.processor.type.comm;
using SharpSqlAnalyzer.pp.stmtformatter;
using SharpSqlAnalyzer.pp.utils;
using SharpSqlAnalyzer.stmt;
using SharpSqlAnalyzer.stmt.mssql;

namespace SharpSqlAnalyzer.pp.processor.type.plsql
{
	// Token: 0x02000236 RID: 566
	public class CreateFuncReturnsTableProcessor : AbstractProcessor
	{
		// Token: 0x060012D8 RID: 4824 RVA: 0x0031AF88 File Offset: 0x00319188
		public override void process(TParseTreeNode nodeArg)
		{
			TStoredProcedureSqlStatement tstoredProcedureSqlStatement = nodeArg as TStoredProcedureSqlStatement;
			this.processDeclare(tstoredProcedureSqlStatement);
			TStatementList bodyStatements = tstoredProcedureSqlStatement.BodyStatements;
			if (bodyStatements == null || bodyStatements.size() == 0)
			{
				return;
			}
			TSourceToken startToken = tstoredProcedureSqlStatement.startToken;
			int num = SourceTokenOperator.curColumnNumberVT(startToken);
			if (bodyStatements.get(0) == null)
			{
				return;
			}
			TSourceToken startToken2 = bodyStatements.get(0).startToken;
			if (startToken2 == null || startToken == null)
			{
				return;
			}
			TSourceToken tsourceToken = SourceTokenSearcher.backforwardSearch(startToken2, 5, "as");
			if (tsourceToken == null)
			{
				return;
			}
			SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createReturnSourceToken());
			SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(num));
			TSourceToken tsourceToken2 = SourceTokenSearcher.backforwardSearch(tsourceToken, tsourceToken.posinlist - tstoredProcedureSqlStatement.startToken.posinlist, "returns");
			if (tsourceToken2 != null)
			{
				SourceTokenOperator.addBefore(this.Option, tsourceToken2, SourceTokenOperator.createReturnSourceToken());
				SourceTokenOperator.addBefore(this.Option, tsourceToken2, SourceTokenOperator.createWhitespaceSourceToken(num));
			}
			for (int i = 0; i < bodyStatements.size(); i++)
			{
				TCustomSqlStatement tcustomSqlStatement = bodyStatements.get(i);
				if (tcustomSqlStatement == null || (tcustomSqlStatement.GSharpSqlParser != null && tcustomSqlStatement.GSharpSqlParser.DbVendor != EDbVendor.dbvmssql))
				{
					break;
				}
				if (!FormatterFactory.isNotNeedFormat(tcustomSqlStatement))
				{
					if (tcustomSqlStatement is TMssqlBlock)
					{
						FormatterFactory.processBlockStmt(this.Option, (TMssqlBlock)tcustomSqlStatement, tsourceToken);
					}
					else
					{
						TSourceToken startToken3 = tcustomSqlStatement.startToken;
						if (startToken3 != null)
						{
							SourceTokenOperator.addBefore(this.Option, startToken3, SourceTokenOperator.createReturnSourceToken());
							SourceTokenOperator.addBefore(this.Option, startToken3, SourceTokenOperator.createWhitespaceSourceToken(num + this.Option.beStyleBlockIndentSize));
							FormatterFactory.processStatement(this.Option, tcustomSqlStatement);
						}
					}
				}
			}
		}

		// Token: 0x060012D9 RID: 4825 RVA: 0x0031B124 File Offset: 0x00319324
		private void processDeclare(TStoredProcedureSqlStatement node)
		{
			TStatementList declareStatements = node.DeclareStatements;
			if (declareStatements == null || declareStatements.size() == 0)
			{
				return;
			}
			TSourceToken startToken = node.startToken;
			int length = SourceTokenOperator.curColumnNumberVT(startToken);
			if (declareStatements.get(0) == null)
			{
				return;
			}
			TSourceToken startToken2 = declareStatements.get(0).startToken;
			if (startToken2 == null || startToken == null)
			{
				return;
			}
			TSourceToken tsourceToken = SourceTokenSearcher.backforwardSearch(startToken2, 5, "is");
			if (tsourceToken == null)
			{
				return;
			}
			SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createReturnSourceToken());
			SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(length));
		}
	}
}
